package logic

import (
	"news/dao/mysql"
	"news/models"
	"news/pkg/jwt"
	"news/pkg/snowflake"
)

func SignUp(p *models.ParamsSignUp) (err error) {
	err = mysql.CheckUserExit(p.Username)
	if err != nil {
		return err
	}
	UserID := snowflake.GenID()
	user := &models.User{
		UserID:   UserID,
		Username: p.Username,
		Password: p.Password,
	}
	err = mysql.InsertUser(user)
	return
}

// 登录的逻辑处理
func Login(u *models.ParamsLogin) (token string, err error) {
	user := &models.User{
		Username: u.Username,
		Password: u.Password,
	}
	err = mysql.Login(user)
	if err != nil {
		return "", err
	}
	return jwt.GenToken(user.UserID, user.Username)
}
